package com.haosheng.testproject.utils;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Http工具类
 */
public class HttpUtils {
    
    private static HttpUtils instance;
    private static OkHttpClient mOkHttpClient;
    private static Request.Builder mBuilder;
    
    private HttpUtils() {
        if (mOkHttpClient == null) {
            synchronized (this) {
                if (mOkHttpClient == null)
                    mOkHttpClient = new OkHttpClient();
            }
        }
        if (mBuilder == null) {
            synchronized (this) {
                if (mBuilder == null)
                    mBuilder = new Request.Builder();
            }
        }
    }
    
    /**
     * 获取实例
     */
    public static HttpUtils getInstance() {
        if (instance == null) {
            synchronized (HttpUtils.class) {
                instance = new HttpUtils();
            }
        }
        return instance;
    }
    
    /**
     * get请求
     */
    public void httpGet(String url, Callback callback) {
        mBuilder.method("GET", null).url(url);
        Request request = mBuilder.build();
        mOkHttpClient.newCall(request).enqueue(callback);
    }
}
